package plgutils

import (
	"dgo/goutils/utils"
	"fmt"
	"gitee.com/ymofen/supervalue"
	"strings"
	"time"
)

/*
*
帐号登陆:

	{
	   "sessionid":"", "clientid":"", "pass":"", "reqservice":"", "fromip":"", "useragent":"", "gwid":"", "logindata":""
	}
*/
func (this *ObjStoragePlg) GetValue(itype int, args ...interface{}) interface{} {
	if itype == 1 {
		if len(args) < 1 {
			return fmt.Errorf("缺少参数")
		}
		strMap := args[0].(*wrapper.SuperValue)
		var task *debugstepsession
		v := utils.CheckGetValInlst(args, 1, nil)
		task, _ = v.(*debugstepsession)
		if task == nil {
			task = &debugstepsession{}
		}
		sessionid := strMap.StringByName("sessionid", "")
		if len(sessionid) == 0 {
			return fmt.Errorf("缺少sessionid参数")
		}
		sess0 := this.checkGetSession(sessionid, false)
		if sess0 != nil {
			return fmt.Errorf("[%s]已经获取了一个对象，SESSIONID是否重复?", sessionid)
		}

		if time.Since(this.startuptime).Seconds() < 60 {
			return fmt.Errorf("首次启动60秒内无法获取对象, 请稍等")
		}

		sess := this.newSession(sessionid)
		sess.ObjectID = strMap.StringByName("id", "")
		sess.StartTime = time.Now()
		sess.LastTime = time.Now()
		sess.RequestData = strMap.StringByName("requestdata", "")
		sess.Match = strMap.StringByName("match", "")
		sess.RequestUser = strMap.StringByName("requestuser", "")
		if len(sess.ObjectID) == 0 {
			task.debugstep = "ObjectGet2"
			usr, err := this.ObjectGet2(sess, task)
			if err != nil {
				return err
			}
			strMap.SetKeyValues("id", sess.ObjectID)
			strMap.SetKeyValue("data", usr.GetObjectData())
		} else {
			task.debugstep = "ObjectGet"
			usr, err := this.ObjectGet(sess, task)
			if err != nil {
				return err
			}
			strMap.SetKeyValue("data", usr.GetObjectData())
		}

		return strMap
	}
	return nil
}

/*
*

	action
	  ping(1):  {"sessionid":"", "id":"XXX", "data":{}}
	  release(2): {"sessionid":"", "id":"XXX", "data":{}, "reason":"", "block-secs":0}
*/
func (this *ObjStoragePlg) OperaAction(action int, args ...interface{}) (err error) {
	if action == 1 {
		if len(args) < 1 {
			return fmt.Errorf("非法的参数")
		}
		argsMap := args[0].(*wrapper.SuperValue)
		sessionid := argsMap.StringByName("sessionid", "")
		id := argsMap.StringByName("id", "")
		if sessionid == "" {
			return fmt.Errorf("参数异常:SessionID为空")
		}
		if id == "" {
			return fmt.Errorf("参数异常:对象ID为空")
		}
		sess, err := this.ObjectPing(sessionid, id)
		if sess != nil {
			sess.PingData = argsMap.StringByName("data", "")
		}
		return err
	} else if action == 2 {
		if len(args) < 1 {
			return fmt.Errorf("非法的参数")
		}
		argsMap := args[0].(*wrapper.SuperValue)
		sessionid := argsMap.StringByName("sessionid", "")
		id := argsMap.StringByName("id", "")
		reason := argsMap.StringByName("reason", "")
		if sessionid == "" {
			return fmt.Errorf("sessionid为空")
		}
		if id == "" {
			return fmt.Errorf("期望账号类型为空")
		}
		blocksecs := argsMap.Int64ByName("block-secs", -1)
		if blocksecs == -1 {
			if strings.Contains(reason, "[不可用]") { // 阻塞60s
				blocksecs = 60
			}
		}
		this.ObjectRelease(sessionid, id, reason, blocksecs)
	}
	return nil
}
